package com.tfswx.jcyw.print.bean;

import javafx.beans.property.*;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 张述江
 * @date 2023-11-03
 */
@Slf4j
@Getter
public class PrintObj {
    private final BooleanProperty visibleProperty = new SimpleBooleanProperty(false);

    private final IntegerProperty totalCountProperty = new SimpleIntegerProperty(0);

    private final IntegerProperty printCountProperty = new SimpleIntegerProperty(0);

    private final DoubleProperty processProperty = new SimpleDoubleProperty(0);

    private final StringProperty percentProcessProperty = new SimpleStringProperty("0%");

    private final StringProperty yeshuProperty = new SimpleStringProperty("第0/0页");

    private boolean isPrint = false;

    public PrintObj() {
        printCountProperty.addListener((observable, oldValue, newValue) -> updateZt());
    }

    private void updateZt() {
        double process = printCountProperty.doubleValue() / totalCountProperty.doubleValue();
        processProperty.set(process);
        percentProcessProperty.set(((int) (process * 10000)) / 100 + "%");
        yeshuProperty.set("第" + printCountProperty.get() + "/" + totalCountProperty.get() + "页");
        if (process == 100) {
            totalCountProperty.setValue(0);
            printCountProperty.setValue(0);
        }
    }

    public void setTotal(int total) {
        totalCountProperty.set(total);
    }

    public void startPrint() {
        processProperty.set(0);
        percentProcessProperty.set("0%");
        yeshuProperty.set("第0/0页");
        updateZt();
        visibleProperty.set(true);
        isPrint = true;
    }

    public void completePrint() {
        visibleProperty.set(false);
        isPrint = false;
    }

    public void cancelPrint() {
//        visibleProperty.set(false);
        completePrint();
    }

    public void print(int yeshu) {
        log.info("{}", yeshu);
        printCountProperty.set(printCountProperty.getValue() + yeshu);
    }
}
